package com.yoso.shop.infrastructure.rabbitmq;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yoso.shop.application.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

/**
 * @author yoso
 * @date 2022/2/19
 */
@Component
@Slf4j
public class ShopMqConsumer {
    @Autowired
    private IOrderService orderService;

    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(value = "${rabbitmq.queue.delay.shop.order}", autoDelete = "true"),
                    exchange = @Exchange(value = "${rabbitmq.exchange.delay.shop.order}", delayed = "true"),
                    key = {"order.confirm"}
            )
    })
    private void delayOrderConfirmConsumer(@NotNull Message message){
        String jsonString = new String(message.getBody(), StandardCharsets.UTF_8);
        log.info("收到订单支付确认消息：{}",jsonString);
        JSONObject orderLogInfo = JSON.parseObject(jsonString);
//        订单支付确认
        orderService.orderPayConfirm((String) orderLogInfo.get("id"));
    }
}
